package com.eu.ebpm.engine.cmd;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.eu.ebpm.engine.entity.deploy.Mission;
import com.eu.ebpm.engine.entity.runtime.ForkVariable;
import com.eu.ebpm.engine.exception.WorkFlowException;
import com.eu.ebpm.engine.express.VariableType;
import com.eu.ebpm.engine.init.EbpmCache;
import com.eu.ebpm.engine.repository.ForkVariableRepository;
import com.eu.ebpm.engine.repository.TaskRepository;
import com.eu.ebpm.engine.service.Task;
import org.springframework.util.StringUtils;

/**
 * 设置分支运行时参数
 * @author 王晓华
 */
@Component
public class SetForkVariableCommand {

	@Autowired
	TaskRepository taskRepository;
	@Autowired
	ForkVariableRepository forkVariableRepository;
	
	public void execute(long taskId, Map<String, Object> vs) {
		
		Task task =  taskRepository.queryById(taskId);
		
		if(StringUtils.isEmpty(task.getAssignee())) {
			throw new WorkFlowException("未签收任务不允许设置运行时参数!");
		}
		
		Mission m = EbpmCache.getMission(task.getMissionId());
		
		if(vs != null) {
			for(String key : vs.keySet()) {
				ForkVariable fv = new ForkVariable(task.getId(), task.getExecutionId(), m.getName(), key, vs.get(key), VariableType.typeOf(vs.get(key)));
				forkVariableRepository.insert(fv);
				forkVariableRepository.insertHis(fv);
			}
		}
		
	}
	
	
	
	
	
	
	
	
	

}
